// src/main/java/com/example/qqviewspringboot/service/DynamicService.java

package com.example.qqviewspringboot.service;

import com.example.qqviewspringboot.data.Dynamic;
import com.example.qqviewspringboot.repository.DynamicRepository;
import com.example.qqviewspringboot.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DynamicService {

    @Autowired
    private DynamicRepository dynamicRepository;

    @Autowired
    private UserRepository userRepository;

    /**
     * 发布动态
     * @param dynamic 动态对象
     * @return 保存后的动态对象
     */
    public Dynamic publishDynamic(Dynamic dynamic) {
        return dynamicRepository.save(dynamic);
    }

    /**
     * 获取指定用户的动态列表
     * @param userId 用户ID
     * @return 动态列表
     */
    public List<Dynamic> getDynamicsByUserId(Long userId) {
        return dynamicRepository.findByUserIdOrderByCreatedAtDesc(userId);
    }

    /**
     * 获取所有用户的动态列表
     * @return 动态列表
     */
    public List<Dynamic> getAllDynamics() {
        return dynamicRepository.findAllByOrderByCreatedAtDesc();
    }
}
